package org.git.movie.exceptionhandler;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.git.movie.util.BaseResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public BaseResponse globalException(HttpServletResponse response, NullPointerException ex){


        log.info("GlobalExceptionHandler...");
        log.info("错误代码："  + response.getStatus());
        BaseResponse result = new BaseResponse("400","异常","");
        return result;
    }

    /**
     * Sentinel流量控制，统一返回码
     * @param response
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(BlockException.class)
    public BaseResponse blockExceptionException(HttpServletResponse response, BlockException ex){


        log.info("GlobalExceptionHandler...");
        log.info("错误代码："  + response.getStatus());
        BaseResponse result = new BaseResponse("400","操作频繁","");
        return result;
    }

}